﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_Modes;
using Zor_Tools;
using Zor_IDAL;

namespace Zor_DAL
{
    public class Index_DAL<T> where T : class
    {
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <returns></returns>
        public static T UserLogin(string userName,string userPass)
        {
            var sql = "select * from [dbo].[Sys_Users] where User_UserName = @UserName and User_Password=@UserPass";
            T rsUser = DapperQuery<T>.DoQueryT(sql, new { UserName = userName, UserPass = userPass });
            return rsUser;
        }
        
        /// <summary>
        /// 密码修改时，旧密码是否正确
        /// </summary>
        /// <param name="oldPass"></param>
        /// <returns></returns>
        public static T IsOldPassMapping(string oldPass)
        {
            string userName = RequestSession.GetSessionUser().User_UserName; //获取当前登录的用户名
            var sql = "select * from [dbo].[Sys_Users] where User_UserName =@userName and  User_Password=@oldPass";
            T rsUser = DapperQuery<T>.DoQueryT(sql, new { userName = userName, oldPass = oldPass });
            return rsUser;
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="newPass"></param>
        /// <returns></returns>
        public static T UpdatePass(string newPass)
        {          
            var userId = RequestSession.GetSessionUser().User_Id; //获取当前用户登录ID
            var sql = "update [dbo].[Sys_Users] set User_Password=@newPass where User_Id =@userId ";
            //var sql = "update [dbo].[Sys_Users] set User_Password ='" + newPass + "' where User_UserName ='" + userName + "' and User_Id = " + userId + "";
            T rsUser = DapperQuery<T>.DoQueryT(sql, new { newPass = newPass, userId = userId });
            return rsUser;
             
        }
    }
}
